let util = {};
util.format = function (date, type = 'YYYY-MM-DD hh:mm:ss') {
    if(!date){
        return ''
    }
    let time = new Date(date);
    let str = type;
    let Week = ['日', '一', '二', '三', '四', '五', '六'];

    str = str.replace(/yyyy|YYYY/, time.getFullYear());
    str = str.replace(/yy|YY/, (time.getYear() % 100) > 9 ? (time.getYear() % 100).toString() : '0' + (time.getYear() % 100));

    str = str.replace(/MM/, (time.getMonth() + 1) > 9 ? (time.getMonth() + 1).toString() : '0' + (time.getMonth() + 1));
    str = str.replace(/M/g, (time.getMonth() + 1));

    str = str.replace(/w|W/g, Week[time.getDay()]);

    str = str.replace(/dd|DD/, time.getDate() > 9 ? time.getDate().toString() : '0' + time.getDate());
    str = str.replace(/d|D/g, time.getDate());

    str = str.replace(/hh|HH/, time.getHours() > 9 ? time.getHours().toString() : '0' + time.getHours());
    str = str.replace(/h|H/g, time.getHours());
    str = str.replace(/mm/, time.getMinutes() > 9 ? time.getMinutes().toString() : '0' + time.getMinutes());
    str = str.replace(/m/g, time.getMinutes());

    str = str.replace(/ss|SS/, time.getSeconds() > 9 ? time.getSeconds().toString() : '0' + time.getSeconds());
    str = str.replace(/s|S/g, time.getSeconds());

    return str;
};
util.RemoveArr = function (arr,uuid) {
    let arr1 = []
    for (var i = 0; i < arr.length; i++) {
        if (arr1.map(x => x[uuid]).indexOf(arr[i][uuid]) == -1) {
            arr1.push(arr[i])
        }
    }
    return arr1
}

util.isNumber = function(val){
    if (parseFloat(val).toString() == "NaN") {
　　　　return false;
　　}else{
　　　　return true;
　　}
}

util.trans = function(arr) {
    let obj = {}
    let result = []
    // {item, actualQty}
    arr.forEach((el) => {
      let cur = obj[el.item]
      if (cur) {
        let index = cur.index
        result[index].actualQty += parseFloat(el.actualQty || 0)
      } else {
        let index = result.length
        obj[el.item] = {
            item:el.item,
            index
        }
        result.push({item:el.item, actualQty:el.actualQty,...el})
      }
    })
    return result
}


util.getXmonthToday = function(type) {
    // type 0 是当天 -1 是上个月   1是下个月
    var now = new Date(); // 可以传值调式 now = new Date(2019,2,30); 今天是3月30号
    var year = now.getFullYear(); //getYear()+1900=getFullYear()
    var month = now.getMonth() + 1; //0-11表示1-12月
    var day = now.getDate(); // 当天日期
    if (parseInt(month) < 10) {
        month = '0' + month;
    }
    if (parseInt(day) < 10) {
        day = '0' + day;
    }
    now = year + '-' + month + '-' + day; // 如果取当月日期可直接 return 返回
    
    let msum = Math.abs(type); 
    
    // console.log(parseInt(msum / 12))
    
    var preMonth  = 0;  //月值
    
    let preSizes = 0; //上月总天数
    
    if(parseInt(msum / 12) === 1){
        //一年
        console.log("一整年")
        preMonth = parseInt(month);
        
        if(parseInt(month) === 12){
        
            preSizes =  new Date(year-1, parseInt(month), 0).getDate();
            if(day > preSizes){
                console.warn(parseInt(year) - 1 + '-'+ preMonth +'-' + preSizes)
                return parseInt(year) - 1 + '-'+ preMonth +'-' + preSizes;
            }else{		
                console.warn(parseInt(year) - 1 + '-'+ preMonth +'-' + day)
                return parseInt(year) - 1 + '-'+ preMonth +'-' + day;
                
            }
        }else{
            console.log("********")
            preSizes =  new Date(year-1, parseInt(month), 0).getDate();
            console.log(preSizes)
            preMonth = preMonth < 10 ? '0' + preMonth : preMonth; // 获取上个月的值	
            if(parseInt(day) > preSizes){
                console.warn(parseInt(year) - 1 + '-'+ preMonth +'-' + preSizes)
                return parseInt(year) - 1 + '-'+ preMonth +'-' + preSizes;
            }else{
                console.warn(parseInt(year) - 1 + '-'+ preMonth +'-' + day)
                return parseInt(year) - 1 + '-'+ preMonth +'-' + day;
            }
        }
        
        
    }else if(parseInt(msum / 12) > 1){
        //两年，三年
        // console.log("两年")
        
        preMonth = parseInt(month);
        
        if(parseInt(month) === 12){
            console.warn(preMonth)
            preSizes =  new Date(year-1, parseInt(month), 0).getDate();
            if(day > preSizes){
                console.warn(parseInt(year) - 1 + '-'+ preMonth +'-' + preSizes)
                return parseInt(year) - parseInt(msum / 12)  + '-'+ preMonth +'-' + preSizes;
            }else{
                console.warn(parseInt(year) - 1 + '-'+ preMonth +'-' + day)
                return parseInt(year) - parseInt(msum / 12)  + '-'+ preMonth +'-' + day;
            }
        }else{
            // console.log("********")
            preSizes =  new Date(year - parseInt(msum / 12), parseInt(month), 0).getDate();
            console.log(preSizes)
            
            console.log(Math.abs(msum) % 12)
            if(Math.abs(msum) % 12 === 0){
                console.log("整年")
                preMonth = preMonth < 10 ? '0' + preMonth : preMonth; // 获取上个月的值
                
                if(parseInt(day) > preSizes){
                    console.warn(parseInt(year) - parseInt(msum / 12)  + '-'+ preMonth +'-' + preSizes)
                    return parseInt(year) - parseInt(msum / 12)  + '-'+ preMonth +'-' + preSizes;
                }else{
                    console.warn(parseInt(year) - parseInt(msum / 12)  + '-'+ preMonth +'-' + day)
                    return parseInt(year) - parseInt(msum / 12)  + '-'+ preMonth +'-' + day;
                }
                
            }else{
                console.log(Math.abs(msum) % 12)
                console.log("两年多")
                preMonth = parseInt(preMonth-(Math.abs(msum) % 12));
                
                preMonth = preMonth < 10 ? '0' + preMonth : preMonth; // 获取上个月的值
                if(parseInt(day) > preSizes){
                    console.warn(parseInt(year) - parseInt(msum / 12)  + '-'+ preMonth +'-' + preSizes)
                    return parseInt(year) - parseInt(msum / 12)  + '-'+ preMonth +'-' + preSizes;
                }else{
                    console.warn(parseInt(year) - parseInt(msum / 12)  + '-'+ preMonth +'-' + day)
                    return parseInt(year) - parseInt(msum / 12)  + '-'+ preMonth +'-' + day;
                }
            }
            
        }
        
        // return parseInt(year) - parseInt(msum / 12) + '-'+ preMonth +'-' + preSizes;
        
        
        
    }else{
        console.log("小于一年")
        //小于一年      
        // preMonth = parseInt(month);	
        console.log(msum)
        console.log(preMonth)					
        
        
        // console.warn(preMonth)
        if(parseInt(month) === msum){
            preMonth = parseInt(month) + 1;	
            preMonth = preMonth < 10 ? '0' + preMonth : preMonth; // 获取上个月的值
            preSizes =  new Date(year-1, parseInt(month), 0).getDate();
            if(day > preSizes){
                return parseInt(year) - 1  + '-'+ preMonth +'-' + preSizes;
            }else{
                console.warn(parseInt(year) - 1  + '-'+ preMonth +'-' + day)
                return parseInt(year) - 1  + '-'+ preMonth +'-' + day;
            }
            
        }else{
            
            preSizes =  new Date(year, parseInt(month), 0).getDate();
            preMonth = parseInt(month) - msum;
            preMonth = preMonth < 10 ? '0' + preMonth : preMonth; // 获取上个月的值
            
            if(day > preSizes){
                console.warn(parseInt(year)  + '-'+ preMonth +'-' + preSizes)
                return parseInt(year)  + '-'+ preMonth +'-' + preSizes;
            }else{
                console.warn(parseInt(year)  + '-'+ preMonth +'-' + day)
                return parseInt(year)  + '-'+ preMonth +'-' + day;
            }
            
        }
        
    }		
    
}

util.exportDown = function(name,res){
    var url = window.URL.createObjectURL(res)
    var link = document.createElement('a')
    link.style.display = 'none'
    link.href = url
    link.setAttribute('id', 'downloadLink')
    link.setAttribute('download',name)
    document.body.appendChild(link)
    link.click()
    // 删除添加的a链接
    var objLink = document.getElementById('downloadLink')
    document.body.removeChild(objLink)
    // 释放内存
    window.URL.revokeObjectURL(url)
}

export { util }